package queue;

/**
 * @author zhaoziqiang
 * @Description:
 * @date 2020/8/31 14:31
 */
public class LinkedListQueue<E> implements Queue<E>{
    private Node head;
    private Node tail;
    private int size;

    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return "Node{" + "e=" + e + ", next=" + next + '}';
        }
    }
    @Override
    public void enqueue(E e) {
        if(tail==null){
            tail=new Node(e);
            head=tail;
        }else{
            tail.next=new Node(e);
            tail=tail.next;
        }
        size++;
    }
    @Override
    public E dequeue() {
        if (size==0)
            throw new IllegalArgumentException("queue is empty");
        Node tmp=head;
        head=head.next;
        tmp.next=null;
        if(head==null)
            tail=null;
        size--;
        return tmp.e;
    }
    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public int getCapacity() {
        return size;
    }



    @Override
    public E getHead() {
        return head.e;
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        //        for (Node cur = dummyHead.next; cur!=null ; cur=cur.next) {
        //            sb.append(cur.e+"->");
        //        }
        Node cur = head;
        sb.append("head ");
        while (cur != null) {
            sb.append(cur.e+"->");
            cur = cur.next;
        }
        sb.append("null ");
        sb.append("tail");
        return sb.toString();
    }
    public static void main(String[] args) {

        LinkedListQueue<Integer> queue = new LinkedListQueue<>();
        for(int i = 0 ; i < 10 ; i ++){
            queue.enqueue(i);
            System.out.println(queue);
            if(i % 3 == 2){
                System.out.println("i="+i+" 出队-start--");
                queue.dequeue();
                System.out.println(queue);
                System.out.println("出队--end-");
            }
        }
    }
}
